let http = require('http');
let _ = require('lodash');
let [host,port] = ['localhost',9999];
var handler = null;
http.createServer((req,res)=>{
    console.log(req.url);
        route(req,res);
}).listen(port);
host = _.isInteger(port)?'localhost:1336':'';
console.log(`Our awesome web server is running at ${host}`);

function route(req,res){
  var handler = new Controller(req,res);
  handler.get('/user',(req,res)=>{
    res.writeHead(200,{
        'Content-Type':'text/plain'
    });
    res.end('user GET method');
});
handler.post('/user',(req,res)=>{
    res.writeHead(200,{
        'Content-Type':'text/plain'
    });
    res.end('user POST method');
});
handler.get('/',(req,res)=>{
    res.writeHead(200,{
        'Content-Type':'text/plain'
    });
    res.end('hello Get method');
})
res.writeHead(200,{
    'Content-Type':'text/plain'
});
res.end("404 not found");

}

function Controller(req,res){
    this.get= function(url,callback){
        if(_.isEqual(req.method,"GET")&&_.isEqual(url,req.url)){
           callback(req,res)
        }
    };
    this.post = (url,callback)=>{
        if(_.isEqual(req.method,"POST")&&_.isEqual(url,req.url)){
            callback(req,res);
        }
    }
};


